package com.jsnkj.basic.util

import android.os.Handler
import com.jsnkj.basic.activity.LoginActivity
import com.jsnkj.basic.bean.ErrorEntity
import com.jsnkj.basic.config.SpConfig
import com.jsnkj.basic.mvvm.BaseMVVMActivity


//服务器返回异常处理
object ErrorResultUtil {


    fun errorResult(activity: BaseMVVMActivity<*, *>, entity: ErrorEntity) {
        if (SpConfig.HTTP_ERROR_CODE == entity.code) {
            SpConfig.isError = true

            activity.showVersionUpdateDialog(
                activity,
                SharePreferencesUtil.getInstance().getSettingEntity().app_version.toString(),
                SharePreferencesUtil.getInstance().getSettingEntity().invites!!
            )
        } else if (SpConfig.HTTP_ERROR_CODE1 == entity.code) {
            com.jsnkj.basic.util.ToastUtils.show("会员已失效，请重新登录")
            Handler().postDelayed({
                activity.startActivity(LoginActivity::class.java)
            }, 1000)
        } else if (SpConfig.HTTP_ERROR_CODE2 == entity.code) {
            SharePreferencesUtil.getInstance().clearAllAdBeanList()
            com.jsnkj.basic.util.ToastUtils.show(entity.message)
        }
    }


    fun errorResultToast(activity: BaseMVVMActivity<*, *>, entity: ErrorEntity) {
        if (SpConfig.HTTP_ERROR_CODE == entity.code) {
            SpConfig.isError = true

            activity.showVersionUpdateDialog(
                activity,
                SharePreferencesUtil.getInstance().getSettingEntity().app_version.toString(),
                SharePreferencesUtil.getInstance().getSettingEntity().invites!!
            )
        } else if (SpConfig.HTTP_ERROR_CODE1 == entity.code) {
            com.jsnkj.basic.util.ToastUtils.show("会员已失效，请重新登录")
            Handler().postDelayed({
                activity.startActivity(LoginActivity::class.java)
            }, 1000)
        } else if (SpConfig.HTTP_ERROR_CODE2 == entity.code) {
            SharePreferencesUtil.getInstance().clearAllAdBeanList()
            com.jsnkj.basic.util.ToastUtils.show(entity.message)
        } else {
            com.jsnkj.basic.util.ToastUtils.show(entity.message)
        }
    }

}